home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Source / GNU / cc / cp-error.c < prev    next >
C/C++ Source or Header  |  1994-05-13  |  28KB  |  1,261 lines

  1. /* Call-backs for C++ error reporting.
  2.    This code is non-reentrant.
  3.    Copyright (C) 1993 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "config.h"
  22. #include "tree.h"
  23. #include "cp-tree.h"
  24. #include "obstack.h"
  25. #include <ctype.h>
  26. #ifdef OBJCPLUS
  27. #include "objc-act.h"
  28. #endif
  29.  
  30. typedef char* cp_printer ();
  31.  
  32. #define C code_as_string
  33. #define D decl_as_string
  34. #define E expr_as_string
  35. #define L language_as_string
  36. #define T type_as_string
  37.  
  38. #define _ (cp_printer *) 0
  39. cp_printer * cp_printers[256] =
  40. /*0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F */
  41.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, /* 0x00 */
  42.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, /* 0x10 */
  43.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, /* 0x20 */
  44.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, /* 0x30 */
  45.   _, _, _, C, D, E, _, _, _, _, _, _, L, _, _, _, /* 0x40 */
  46.   _, _, _, _, T, _, _, _, _, _, _, _, _, _, _, _, /* 0x50 */
  47.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, /* 0x60 */
  48.   _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, /* 0x70 */
  49. };
  50. #undef C
  51. #undef D
  52. #undef E
  53. #undef L
  54. #undef T
  55. #undef _
  56.  
  57. #define obstack_chunk_alloc xmalloc
  58. #define obstack_chunk_free free
  59.  
  60. /* Obstack where we build text strings for overloading, etc.  */
  61. static struct obstack scratch_obstack;
  62. static char *scratch_firstobj;
  63.  
  64. # define OB_INIT() (scratch_firstobj ? (obstack_free (&scratch_obstack, scratch_firstobj), 0) : 0)
  65. # define OB_PUTC(C) (obstack_1grow (&scratch_obstack, (C)))
  66. # define OB_PUTC2(C1,C2)    \
  67.   (obstack_1grow (&scratch_obstack, (C1)), obstack_1grow (&scratch_obstack, (C2)))
  68. # define OB_PUTS(S) (obstack_grow (&scratch_obstack, (S), sizeof (S) - 1))
  69. # define OB_PUTID(ID)  \
  70.   (obstack_grow (&scratch_obstack, IDENTIFIER_POINTER (ID),    \
  71.          IDENTIFIER_LENGTH (ID)))
  72. # define OB_PUTCP(S) (obstack_grow (&scratch_obstack, (S), strlen (S)))
  73. # define OB_FINISH() (obstack_1grow (&scratch_obstack, '\0'))
  74. # define OB_PUTI(CST) do { sprintf (digit_buffer, "%d", (CST)); \
  75.                OB_PUTCP (digit_buffer); } while (0)
  76.  
  77. # define NEXT_CODE(t) (TREE_CODE (TREE_TYPE (t)))
  78.  
  79. static void dump_type (), dump_decl (), dump_function_decl ();
  80. static void dump_expr (), dump_unary_op (), dump_binary_op ();
  81. static void dump_aggr_type (), dump_type_prefix (), dump_type_suffix ();
  82. static void dump_function_name ();
  83.  
  84. void
  85. init_error ()
  86. {
  87.   gcc_obstack_init (&scratch_obstack);
  88.   scratch_firstobj = (char *)obstack_alloc (&scratch_obstack, 0);
  89. }
  90.  
  91. /* Counter to help build parameter names in case they were omitted.  */
  92. static int dummy_name;
  93.  
  94. enum pad { none, before, after };
  95.  
  96. static void
  97. dump_readonly_or_volatile (t, p)
  98.      tree t;
  99.      enum pad p;
  100. {
  101.   if (TYPE_READONLY (t) || TYPE_VOLATILE (t))
  102.     {
  103.       if (p == before) OB_PUTC (' ');
  104.       if (TYPE_READONLY (t))
  105.     OB_PUTS ("const");
  106.       if (TYPE_VOLATILE (t))
  107.     OB_PUTS ("volatile");
  108.       if (p == after) OB_PUTC (' ');
  109.     }
  110. }
  111.  
  112. /* This must be large enough to hold any printed integer or floating-point
  113.    value.  */
  114. static char digit_buffer[128];
  115.  
  116. /* Dump into the obstack a human-readable equivalent of TYPE. */
  117. static void
  118. dump_type (t, v)
  119.      tree t;
  120.      int v;            /* verbose? */
  121. {
  122.   if (t == NULL_TREE)
  123.     return;
  124.   
  125.   if (TYPE_PTRMEMFUNC_P (t))
  126.     goto offset_type;
  127.  
  128.   switch (TREE_CODE (t))
  129.     {
  130.     case ERROR_MARK:
  131.       OB_PUTS ("<error>");
  132.       break;
  133.  
  134.     case UNKNOWN_TYPE:
  135.       OB_PUTS ("<unknown type>");
  136.       break;
  137.  
  138.     case TREE_LIST:
  139.       /* i.e. function taking no arguments */
  140.       if (t != void_list_node)
  141.     {
  142.       dump_type (TREE_VALUE (t), v);
  143.       /* Can this happen other than for default arguments? */
  144.       if (TREE_PURPOSE (t) && v)
  145.         {
  146.           OB_PUTS (" = ");
  147.           dump_expr (TREE_PURPOSE (t));
  148.         }
  149.       if (TREE_CHAIN (t))
  150.         {
  151.           if (TREE_CHAIN (t) != void_list_node)
  152.         {
  153.           OB_PUTC2 (',', ' ');
  154.           dump_type (TREE_CHAIN (t), v);
  155.         }
  156.         }
  157.       else OB_PUTS (" ...");
  158.     }
  159.       break;
  160.  
  161.     case IDENTIFIER_NODE:
  162.       OB_PUTID (t);
  163.       break;
  164.  
  165.     case TREE_VEC:
  166.       dump_type (BINFO_TYPE (t), v);
  167.       break;
  168.  
  169.     case RECORD_TYPE:
  170.     case UNION_TYPE:
  171.     case ENUMERAL_TYPE:
  172.       dump_aggr_type (t, v);
  173.       break;
  174.  
  175.     case TYPE_DECL:
  176.       dump_readonly_or_volatile (t, after);
  177.       OB_PUTID (DECL_NAME (t));
  178.       break;
  179.  
  180.     case INTEGER_TYPE:
  181.       if (!TREE_UNSIGNED (TYPE_MAIN_VARIANT (t)) && TREE_UNSIGNED (t))
  182.     OB_PUTS ("unsigned ");
  183.       else if (TREE_UNSIGNED (TYPE_MAIN_VARIANT (t)) && !TREE_UNSIGNED (t))
  184.     OB_PUTS ("signed ");
  185.  
  186.       /* fall through.  */
  187.     case REAL_TYPE:
  188.     case VOID_TYPE:
  189.       dump_readonly_or_volatile (t, after);
  190.       OB_PUTID (TYPE_IDENTIFIER (t));
  191.       break;
  192.  
  193.     case TEMPLATE_TYPE_PARM:
  194.       OB_PUTS ("<template type parm ");
  195.       OB_PUTID (TYPE_IDENTIFIER (t));
  196.       OB_PUTC ('>');
  197.       break;
  198.  
  199.     case UNINSTANTIATED_P_TYPE:
  200.       OB_PUTID (DECL_NAME (UPT_TEMPLATE (t)));
  201.       OB_PUTS ("<...>");
  202.       break;
  203.  
  204.       /* This is not always necessary for pointers and such, but doing this
  205.      reduces code size.  */
  206.     case POINTER_TYPE:
  207.     case ARRAY_TYPE:
  208.     case REFERENCE_TYPE:
  209.     case OFFSET_TYPE:
  210.     offset_type:
  211.     case FUNCTION_TYPE:
  212.     case METHOD_TYPE:
  213.       dump_type_prefix (t, v);
  214.       dump_type_suffix (t, v);
  215.       break;
  216.  
  217.     default:
  218.       my_friendly_abort (68);
  219.       
  220.     }
  221. }
  222.  
  223. /* Print out a class declaration, in the form `class foo'. */
  224. static void
  225. dump_aggr_type (t, v)
  226.      tree t;
  227.      int v;            /* verbose? */
  228. {
  229.   tree name;
  230.   char *variety;
  231.  
  232.   if (TREE_CODE (t) == ENUMERAL_TYPE)
  233.     variety = "enum";
  234.   else if (TREE_CODE (t) == UNION_TYPE)
  235.     variety = "union";
  236.   else if (TYPE_LANG_SPECIFIC (t) && CLASSTYPE_DECLARED_CLASS (t))
  237.     variety = "class";
  238.   else
  239.     variety = "struct";
  240.  
  241.   dump_readonly_or_volatile (t, after);
  242.  
  243.   if (v)
  244.     {
  245.       OB_PUTCP (variety);
  246.       OB_PUTC (' ');
  247.     }
  248.   
  249.   name = TYPE_NAME (t);
  250.  
  251.   if (DECL_CONTEXT (name))
  252.     {
  253.       /* FUNCTION_DECL or RECORD_TYPE */
  254.       dump_decl (DECL_CONTEXT (name), 0);
  255.       OB_PUTC2 (':', ':');
  256.     }
  257.  
  258.   /* kludge around wierd behavior on g++.brendan/line1.C */
  259.   if (TREE_CODE (name) != IDENTIFIER_NODE)
  260.     name = DECL_NAME (name);
  261.  
  262.   if (ANON_AGGRNAME_P (name))
  263.     {
  264.       OB_PUTS ("<anonymous");
  265.       if (!v)
  266.     {
  267.       OB_PUTC (' ');
  268.       OB_PUTCP (variety);
  269.     }
  270.       OB_PUTC ('>');
  271.     }
  272.   else
  273.     OB_PUTID (name);
  274. }
  275.  
  276. /* Dump into the obstack the initial part of the output for a given type.
  277.    This is necessary when dealing with things like functions returning
  278.    functions.  Examples:
  279.  
  280.    return type of `int (* fee ())()': pointer -> function -> int.  Both
  281.    pointer (and reference and offset) and function (and member) types must
  282.    deal with prefix and suffix.
  283.  
  284.    Arrays must also do this for DECL nodes, like int a[], and for things like
  285.    int *[]&.  */
  286.  
  287. static void
  288. dump_type_prefix (t, v)
  289.      tree t;
  290.      int v;            /* verbosity */
  291. {
  292.   if (TYPE_PTRMEMFUNC_P (t))
  293.     {
  294.       t = TYPE_PTRMEMFUNC_FN_TYPE (t);
  295.       goto offset_type;
  296.     }
  297.   
  298.   switch (TREE_CODE (t))
  299.     {
  300.     case POINTER_TYPE:
  301.       {
  302.     tree sub = TREE_TYPE (t);
  303.     
  304. #if 0
  305.     if (doing_objc_thang) 
  306.       {
  307.         static tree id_type = 0, sel_type;
  308.         
  309.         /* initialize id_type and sel_type */
  310.         if (!id_type)
  311.           {
  312.         tree id, ref;
  313.         
  314.         id = get_identifier ("objc_object");
  315.         ref = xref_tag (RECORD_TYPE, id);
  316.         id_type = build_pointer_type (ref);
  317.         
  318.         id = get_identifier ("objc_selector");
  319.         ref = xref_tag (RECORD_TYPE, id);
  320.         sel_type = build_pointer_type (ref);
  321.           }
  322.  
  323.         if (TYPE_MAIN_VARIANT (t) == TYPE_MAIN_VARIANT (id_type))
  324.           {
  325.         OB_PUTS ("id ");
  326.         dump_readonly_or_volatile (t, none);
  327.         return;
  328.           }
  329.         else if (TYPE_MAIN_VARIANT (t) == TYPE_MAIN_VARIANT (sel_type))
  330.           {
  331.         OB_PUTS ("SEL ");
  332.         dump_readonly_or_volatile (t, none);
  333.         return;
  334.           }
  335.       }
  336. #endif
  337.  
  338.     dump_type_prefix (sub, v);
  339.     /* A tree for a member pointer looks like pointer to offset,
  340.        so let the OFFSET_TYPE case handle it.  */
  341.     if (TREE_CODE (sub) != OFFSET_TYPE)
  342.       {
  343.         switch (TREE_CODE (sub))
  344.           {
  345.         /* We don't want int ( *)() */
  346.           case FUNCTION_TYPE:
  347.           case METHOD_TYPE:
  348.         break;
  349.         
  350.           case POINTER_TYPE:
  351.         /* We don't want "char * *" */
  352.         if (! (TYPE_READONLY (sub) || TYPE_VOLATILE (sub)))
  353.           break;
  354.         /* But we do want "char *const *" */
  355.         
  356.           default:
  357.         OB_PUTC (' ');
  358.           }
  359.         OB_PUTC ('*');
  360.         dump_readonly_or_volatile (t, none);
  361.       }
  362.       }
  363.       break;
  364.  
  365.     case REFERENCE_TYPE:
  366.       {
  367.     tree sub = TREE_TYPE (t);
  368.     dump_type_prefix (sub, v);
  369.  
  370.     switch (TREE_CODE (sub))
  371.       {
  372.       case POINTER_TYPE:
  373.         /* We don't want "char * &" */
  374.         if (! (TYPE_READONLY (sub) || TYPE_VOLATILE (sub)))
  375.           break;
  376.         /* But we do want "char *const &" */
  377.  
  378.       default:
  379.         OB_PUTC (' ');
  380.       }
  381.       }
  382.       OB_PUTC ('&');
  383.       dump_readonly_or_volatile (t, none);
  384.       break;
  385.  
  386.     case OFFSET_TYPE:
  387.     offset_type:
  388.       dump_type_prefix (TREE_TYPE (t), v);
  389.       if (NEXT_CODE (t) != FUNCTION_TYPE && NEXT_CODE (t) != METHOD_TYPE)
  390.     OB_PUTC (' ');
  391.       if (TREE_CODE (t) == OFFSET_TYPE)
  392.     dump_type (TYPE_OFFSET_BASETYPE (t), 0);
  393.       else            /* pointer to member function */
  394.     dump_type (TYPE_METHOD_BASETYPE (TREE_TYPE (t)), 0);
  395.       OB_PUTC2 (':', ':');
  396.       OB_PUTC ('*');
  397.       dump_readonly_or_volatile (t, none);
  398.       break;
  399.  
  400.       /* Can only be reached through function pointer -- this would not be
  401.          correct if FUNCTION_DECLs used it.  */
  402.     case FUNCTION_TYPE:
  403.     case METHOD_TYPE:
  404.       dump_type_prefix (TREE_TYPE (t), v);
  405.       OB_PUTC2 (' ', '(');
  406.       break;
  407.  
  408.     case ARRAY_TYPE:
  409.       dump_type_prefix (TREE_TYPE (t), v);
  410.       break;
  411.  
  412.     case ENUMERAL_TYPE:
  413.     case ERROR_MARK:
  414.     case IDENTIFIER_NODE:
  415.     case INTEGER_TYPE:
  416.     case REAL_TYPE:
  417.     case RECORD_TYPE:
  418.     case TEMPLATE_TYPE_PARM:
  419.     case TREE_LIST:
  420.     case TYPE_DECL:
  421.     case TREE_VEC:
  422.     case UNINSTANTIATED_P_TYPE:
  423.     case UNION_TYPE:
  424.     case UNKNOWN_TYPE:
  425.     case VOID_TYPE:
  426.       dump_type (t, v);
  427.       break;
  428.       
  429.     default:
  430.       my_friendly_abort (65);
  431.     }
  432. }
  433.  
  434. static void
  435. dump_type_suffix (t, v)
  436.      tree t;
  437.      int v;            /* verbose? */
  438. {
  439.   if (TYPE_PTRMEMFUNC_P (t))
  440.     t = TYPE_PTRMEMFUNC_FN_TYPE (t);
  441.  
  442.   switch (TREE_CODE (t))
  443.     {
  444.     case POINTER_TYPE:
  445.     case REFERENCE_TYPE:
  446.     case OFFSET_TYPE:
  447.       dump_type_suffix (TREE_TYPE (t), v);
  448.       break;
  449.  
  450.       /* Can only be reached through function pointer */
  451.     case FUNCTION_TYPE:
  452.     case METHOD_TYPE:
  453.       {
  454.     tree arg;
  455.     OB_PUTC2 (')', '(');
  456.     arg = TYPE_ARG_TYPES (t);
  457.     if (TREE_CODE (t) == METHOD_TYPE)
  458.       arg = TREE_CHAIN (arg);
  459.  
  460.     if (arg)
  461.       dump_type (arg, v);
  462.     else
  463.       OB_PUTS ("...");
  464.     OB_PUTC (')');
  465.     if (TREE_CODE (t) == METHOD_TYPE)
  466.       dump_readonly_or_volatile
  467.         (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))), before);
  468.     dump_type_suffix (TREE_TYPE (t), v);
  469.     break;
  470.       }
  471.  
  472.     case ARRAY_TYPE:
  473.       OB_PUTC ('[');
  474.       if (TYPE_DOMAIN (t))
  475.     OB_PUTI (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (t))) + 1);
  476.       OB_PUTC (']');
  477.       dump_type_suffix (TREE_TYPE (t), v);
  478.       break;
  479.       
  480.     case ENUMERAL_TYPE:
  481.     case ERROR_MARK:
  482.     case IDENTIFIER_NODE:
  483.     case INTEGER_TYPE:
  484.     case REAL_TYPE:
  485.     case RECORD_TYPE:
  486.     case TEMPLATE_TYPE_PARM:
  487.     case TREE_LIST:
  488.     case TYPE_DECL:
  489.     case TREE_VEC:
  490.     case UNINSTANTIATED_P_TYPE:
  491.     case UNION_TYPE:
  492.     case UNKNOWN_TYPE:
  493.     case VOID_TYPE:
  494.       break;
  495.  
  496.     default:
  497.       my_friendly_abort (67);
  498.     }
  499. }
  500.  
  501. /* Return a function declaration which corresponds to the IDENTIFIER_NODE
  502.    argument.  */
  503. tree
  504. ident_fndecl (t)
  505.      tree t;
  506. {
  507.   tree n = IDENTIFIER_GLOBAL_VALUE (t);
  508.  
  509.   if (TREE_CODE (n) == FUNCTION_DECL)
  510.     return n;
  511.   else if (TREE_CODE (n) == TREE_LIST
  512.        && TREE_CODE (TREE_VALUE (n)) == FUNCTION_DECL)
  513.     return TREE_VALUE (n);
  514.   else
  515.     my_friendly_abort (66);
  516. }
  517.  
  518. #ifndef NO_DOLLAR_IN_LABEL
  519. #  define GLOBAL_THING "_GLOBAL_$"
  520. #else
  521. #  ifndef NO_DOT_IN_LABEL
  522. #    define GLOBAL_THING "_GLOBAL_."
  523. #  else
  524. #    define GLOBAL_THING "_GLOBAL__"
  525. #  endif
  526. #endif
  527.  
  528. #define GLOBAL_IORD_P(NODE) \
  529.   !strncmp(IDENTIFIER_POINTER(NODE),GLOBAL_THING,sizeof(GLOBAL_THING)-1)
  530.  
  531. void
  532. dump_global_iord (t)
  533.      tree t;
  534. {
  535.   char *name = IDENTIFIER_POINTER (t);
  536.  
  537.   OB_PUTS ("(static ");
  538.   if (name [sizeof (GLOBAL_THING) - 1] == 'I')
  539.     OB_PUTS ("initializers");
  540.   else if (name [sizeof (GLOBAL_THING) - 1] == 'D')
  541.     OB_PUTS ("destructors");
  542.   else
  543.     my_friendly_abort (352);
  544.   
  545.   OB_PUTS (" for ");
  546.   OB_PUTCP (input_filename);
  547.   OB_PUTC (')');
  548. }
  549.  
  550. static void
  551. dump_decl (t, v)
  552.      tree t;
  553.      int v;            /* verbosity */
  554. {
  555.   if (t == NULL_TREE)
  556.     return;
  557.  
  558.   switch (TREE_CODE (t))
  559.     {
  560.     case ERROR_MARK:
  561.       OB_PUTS (" /* decl error */ ");
  562.       break;
  563.  
  564.     case VAR_DECL:
  565.       if (VTABLE_NAME_P (DECL_NAME (t)))
  566.     {
  567.       OB_PUTS ("vtable for ");
  568.       dump_type (DECL_CONTEXT (t), v);
  569.       break;
  570.     }
  571.       /* else fall through */
  572.     case FIELD_DECL:
  573.     case PARM_DECL:
  574.       if (v)
  575.     {
  576.       dump_type_prefix (TREE_TYPE (t), v);
  577.       OB_PUTC(' ');
  578.     }
  579.       /* DECL_CLASS_CONTEXT isn't being set in some cases.  Hmm...  */
  580.       if (TREE_CODE (t) == FIELD_DECL
  581.       || (TREE_CODE (t) == VAR_DECL && DECL_CONTEXT (t)
  582.           && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't'))
  583.     {
  584.       dump_type (DECL_CONTEXT (t), 0);
  585.       OB_PUTC2(':', ':');
  586.     }
  587.       if (DECL_NAME (t))
  588.     dump_decl (DECL_NAME (t), v);
  589.       else
  590.     OB_PUTS ("<anon>");
  591.       if (v) dump_type_suffix (TREE_TYPE (t), v);
  592.       break;
  593.  
  594.     case ARRAY_REF:
  595.       dump_decl (TREE_OPERAND (t, 0), v);
  596.       OB_PUTC ('[');
  597.       dump_decl (TREE_OPERAND (t, 1), v);
  598.       OB_PUTC (']');
  599.       break;
  600.  
  601.       /* So that we can do dump_decl in dump_aggr_type and have it work for
  602.      both class and function scope.  */
  603.     case RECORD_TYPE:
  604.     case UNION_TYPE:
  605.     case ENUMERAL_TYPE:
  606.       dump_type (t, v);
  607.       break;
  608.  
  609.     case TYPE_DECL:
  610.       dump_type (TREE_TYPE (t), v);
  611.       break;
  612.  
  613.     case TYPE_EXPR:
  614.       my_friendly_abort (69);
  615.       break;
  616.  
  617.       /* These special cases are duplicated here so that other functions
  618.      can feed identifiers to cp_error and get them demangled properly. */
  619.     case IDENTIFIER_NODE:
  620.       if (DESTRUCTOR_NAME_P (t))
  621.     {
  622.       OB_PUTC ('~');
  623.       dump_decl (DECL_NAME (ident_fndecl (t)), 0);
  624.     }
  625.       else if (IDENTIFIER_TYPENAME_P (t))
  626.     {
  627.       OB_PUTS ("operator ");
  628.       /* Not exactly IDENTIFIER_TYPE_VALUE.  */
  629.       dump_type (TREE_TYPE (t), 0);
  630.       break;
  631.     }
  632.       else if (IDENTIFIER_OPNAME_P (t))
  633.     {
  634.       char *name_string = operator_name_string (t);
  635.       OB_PUTS ("operator ");
  636.       OB_PUTCP (name_string);
  637.     }
  638.       else
  639.     OB_PUTID (t);
  640.       break;
  641.  
  642.     case FUNCTION_DECL:
  643.       if (GLOBAL_IORD_P (DECL_ASSEMBLER_NAME (t)))
  644.     dump_global_iord (DECL_ASSEMBLER_NAME (t));
  645.       else
  646.     dump_function_decl (t, v);
  647.       break;
  648.  
  649.     case TEMPLATE_DECL:
  650.       switch (NEXT_CODE (t))
  651.     {
  652.     case METHOD_TYPE:
  653.     case FUNCTION_TYPE:
  654.       dump_function_decl (t, v);
  655.       break;
  656.  
  657.     default:
  658.       my_friendly_abort (353);
  659.     }
  660.       break;
  661.  
  662.     case LABEL_DECL:
  663.       OB_PUTID (DECL_NAME (t));
  664.       break;
  665.  
  666.     case CONST_DECL:
  667.       if (NEXT_CODE (t) == ENUMERAL_TYPE)
  668.     {
  669.       if (DECL_CONTEXT (t))
  670.         {
  671.           dump_decl (DECL_CONTEXT (t), 0);
  672.           OB_PUTC2 (':', ':');
  673.         }
  674.       OB_PUTID (DECL_NAME (t));
  675.     }
  676.       else
  677.     dump_expr (DECL_INITIAL (t), 0);
  678.       break;
  679.  
  680. #ifdef OBJCPLUS
  681.     case INSTANCE_METHOD_DECL:
  682.     case CLASS_METHOD_DECL:
  683.     dump_decl (METHOD_DEFINITION (t), v);
  684.     break;
  685. #endif
  686.  
  687.     default:
  688.       my_friendly_abort (70);
  689.     }
  690. }
  691.  
  692. /* Pretty printing for announce_function.  T is the declaration of the
  693.    function we are interested in seeing.  V is non-zero if we should print
  694.    the type that this function returns.  */
  695.  
  696. static void
  697. dump_function_decl (t, v)
  698.      tree t;
  699.      int v;
  700. {
  701.   tree name = DECL_ASSEMBLER_NAME (t);
  702.   tree fntype = TREE_TYPE (t);
  703.   tree parmtypes = TYPE_ARG_TYPES (fntype);
  704.   tree cname = NULL_TREE;
  705.   int spaces = 0;
  706.  
  707.   if (DECL_CLASS_CONTEXT (t))
  708.     cname = DECL_CLASS_CONTEXT (t);
  709.   /* this is for partially instantiated template methods */
  710.   else if (TREE_CODE (fntype) == METHOD_TYPE)
  711.     cname = TREE_TYPE (TREE_VALUE (parmtypes));
  712.  
  713.   if (v)
  714.     {
  715.       if (DECL_STATIC_FUNCTION_P (t))
  716.     OB_PUTS ("static ");
  717.     
  718.       if (! IDENTIFIER_TYPENAME_P (name))
  719.     {
  720.       dump_type_prefix (TREE_TYPE (fntype), 1);
  721.       OB_PUTC (' ');
  722.     }
  723.     }
  724.  
  725.   if (cname)
  726.     {
  727.       dump_type (cname, 0);
  728.       OB_PUTC2 (':', ':');
  729.       if (TREE_CODE (fntype) == METHOD_TYPE && parmtypes)
  730.     parmtypes = TREE_CHAIN (parmtypes);
  731.       if (DECL_CONSTRUCTOR_FOR_VBASE_P (t))
  732.     /* Skip past "in_charge" identifier.  */
  733.     parmtypes = TREE_CHAIN (parmtypes);
  734.     }
  735.  
  736.   if (DESTRUCTOR_NAME_P (name))
  737.     parmtypes = TREE_CHAIN (parmtypes);
  738.   
  739.   dump_function_name (t);
  740.   
  741.   OB_PUTC ('(');
  742.  
  743.   if (parmtypes)
  744.     dump_type (parmtypes, v);
  745.   else
  746.     OB_PUTS ("...");
  747.  
  748.   OB_PUTC (')');
  749.  
  750.   if (v && ! IDENTIFIER_TYPENAME_P (name))
  751.     dump_type_suffix (TREE_TYPE (fntype), 1);
  752.  
  753.   if (TREE_CODE (fntype) == METHOD_TYPE)
  754.     dump_readonly_or_volatile
  755.       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype))), before);
  756. }
  757.  
  758. /* Handle the function name for a FUNCTION_DECL node, grokking operators
  759.    and destructors properly.  */
  760. static void
  761. dump_function_name (t)
  762.      tree t;
  763. {
  764.   tree name = DECL_NAME (t);
  765.  
  766.   /* There ought to be a better way to find out whether or not something is
  767.      a destructor.  */
  768.   if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t)))
  769.     {
  770.       OB_PUTC ('~');
  771.       dump_decl (name, 0);
  772.     }
  773.   else if (IDENTIFIER_TYPENAME_P (name))
  774.     {
  775.       /* This cannot use the hack that the operator's return
  776.      type is stashed off of its name because it may be
  777.      used for error reporting.  In the case of conflicting
  778.      declarations, both will have the same name, yet
  779.      the types will be different, hence the TREE_TYPE field
  780.      of the first name will be clobbered by the second.  */
  781.       OB_PUTS ("operator ");
  782.       dump_type (TREE_TYPE (TREE_TYPE (t)), 0);
  783.     }
  784.   else if (IDENTIFIER_OPNAME_P (name))
  785.     {
  786.       char *name_string = operator_name_string (name);
  787.       OB_PUTS ("operator ");
  788.       OB_PUTCP (name_string);
  789.     }
  790.   else
  791.     dump_decl (name, 0);
  792. }
  793.  
  794. static void
  795. dump_char (c)
  796.      char c;
  797. {
  798.   switch (c)
  799.     {
  800.     case '\n':
  801.       OB_PUTS ("\\n");
  802.       break;
  803.     case '\t':
  804.       OB_PUTS ("\\t");
  805.       break;
  806.     case '\v':
  807.       OB_PUTS ("\\v");
  808.       break;
  809.     case '\b':
  810.       OB_PUTS ("\\b");
  811.       break;
  812.     case '\r':
  813.       OB_PUTS ("\\r");
  814.       break;
  815.     case '\f':
  816.       OB_PUTS ("\\f");
  817.       break;
  818.     case '\a':
  819.       OB_PUTS ("\\a");
  820.       break;
  821.     case '\\':
  822.       OB_PUTS ("\\\\");
  823.       break;
  824.     case '\'':
  825.       OB_PUTS ("\\'");
  826.       break;
  827.     case '\"':
  828.       OB_PUTS ("\\\"");
  829.       break;
  830.     default:
  831.       if (isprint (c))
  832.     OB_PUTC (c);
  833.       else
  834.     {
  835.       sprintf (digit_buffer, "\\%03o", (int) c);
  836.       OB_PUTCP (digit_buffer);
  837.     }
  838.     }
  839. }
  840.  
  841. /* Print out a list of initializers (subr of dump_expr) */
  842. static void
  843. dump_expr_list (l)
  844.      tree l;
  845. {
  846.   while (l)
  847.     {
  848.       dump_expr (TREE_VALUE (l), 0);
  849.       if (TREE_CHAIN (l))
  850.     OB_PUTC2 (',', ' ');
  851.       l = TREE_CHAIN (l);
  852.     }
  853. }
  854.  
  855. /* Print out an expression */
  856. static void
  857. dump_expr (t, nop)
  858.      tree t;
  859.      int nop;            /* suppress parens */
  860. {
  861.   switch (TREE_CODE (t))
  862.     {
  863.     case VAR_DECL:
  864.     case PARM_DECL:
  865.     case FIELD_DECL:
  866.     case CONST_DECL:
  867.     case FUNCTION_DECL:
  868.       dump_decl (t, 0);
  869.       break;
  870.  
  871.     case INTEGER_CST:
  872.       {
  873.     tree type = TREE_TYPE (t);
  874.     my_friendly_assert (type != 0, 81);
  875.  
  876.     /* If it's an enum, output its tag, rather than its value.  */
  877.     if (TREE_CODE (type) == ENUMERAL_TYPE)
  878.       {
  879.         char *p = enum_name_string (t, type);
  880.         OB_PUTCP (p);
  881.       }
  882.     else if (type == char_type_node
  883.          || type == signed_char_type_node
  884.          || type == unsigned_char_type_node)
  885.       {
  886.         OB_PUTC ('\'');
  887.         dump_char (TREE_INT_CST_LOW (t));
  888.         OB_PUTC ('\'');
  889.       }
  890.     else if (TREE_INT_CST_HIGH (t)
  891.          != (TREE_INT_CST_LOW (t) >> (HOST_BITS_PER_WIDE_INT - 1)))
  892.       {
  893.         tree val = t;
  894.         if (TREE_INT_CST_HIGH (val) < 0)
  895.           {
  896.         OB_PUTC ('-');
  897.         val = build_int_2 (~TREE_INT_CST_LOW (val),
  898.                    -TREE_INT_CST_HIGH (val));
  899.           }
  900.         /* Would "%x%0*x" or "%x%*0x" get zero-padding on all
  901.            systems?  */
  902.         {
  903.           static char format[10]; /* "%x%09999x\0" */
  904.           if (!format[0])
  905.         sprintf (format, "%%x%%0%dx", HOST_BITS_PER_INT / 4);
  906.           sprintf (digit_buffer, format, TREE_INT_CST_HIGH (val),
  907.                TREE_INT_CST_LOW (val));
  908.           OB_PUTCP (digit_buffer);
  909.         }
  910.       }
  911.     else
  912.       OB_PUTI (TREE_INT_CST_LOW (t));
  913.       }
  914.       break;
  915.  
  916.     case REAL_CST:
  917. #ifndef REAL_IS_NOT_DOUBLE
  918.       sprintf (digit_buffer, "%g", TREE_REAL_CST (t));
  919. #else
  920.       {
  921.     int i;
  922.     char *p = (char *) &TREE_REAL_CST (t);
  923.     sprintf (digit_buffer, "0x");
  924.     for (i = 0; i < sizeof TREE_REAL_CST (t); i++)
  925.       sprintf (digit_buffer + 2 + 2*i, "%02x", *p++);
  926.       }
  927. #endif
  928.       OB_PUTCP (digit_buffer);
  929.       break;
  930.  
  931.     case STRING_CST:
  932.       {
  933.     char *p = TREE_STRING_POINTER (t);
  934.     int len = TREE_STRING_LENGTH (t) - 1;
  935.     int i;
  936.  
  937.     OB_PUTC ('\"');
  938.     for (i = 0; i < len; i++)
  939.       dump_char (p[i]);
  940.     OB_PUTC ('\"');
  941.       }
  942.       break;
  943.  
  944.     case COMPOUND_EXPR:
  945.       dump_binary_op (",", t);
  946.       break;
  947.  
  948.     case COND_EXPR:
  949.       OB_PUTC ('(');
  950.       dump_expr (TREE_OPERAND (t, 0), 0);
  951.       OB_PUTS (" ? ");
  952.       dump_expr (TREE_OPERAND (t, 1), 0);
  953.       OB_PUTS (" : ");
  954.       dump_expr (TREE_OPERAND (t, 2), 0);
  955.       OB_PUTC (')');
  956.       break;
  957.  
  958.     case SAVE_EXPR:
  959.       if (TREE_HAS_CONSTRUCTOR (t))
  960.     {
  961.       OB_PUTS ("new ");
  962.       dump_type (TREE_TYPE (TREE_TYPE (t)), 0);
  963.       PARM_DECL_EXPR (t) = 1;
  964.     }
  965.       else
  966.     {
  967.       sorry ("operand of SAVE_EXPR not understood");
  968.       goto error;
  969.     }
  970.       break;
  971.  
  972.     case NEW_EXPR:
  973.       OB_PUTID (TYPE_IDENTIFIER (TREE_TYPE (t)));
  974.       OB_PUTC ('(');
  975.       dump_expr_list (TREE_CHAIN (TREE_OPERAND (t, 1)));
  976.       OB_PUTC (')');
  977.       break;
  978.  
  979.     case CALL_EXPR:
  980.       dump_expr (TREE_OPERAND (t, 0), 0);
  981.       OB_PUTC2 (' ', '(');
  982.       dump_expr_list (TREE_OPERAND (t, 1));
  983.       OB_PUTC (')');
  984.       break;
  985.  
  986.     case WITH_CLEANUP_EXPR:
  987.       /* Note that this only works for G++ cleanups.  If somebody
  988.      builds a general cleanup, there's no way to represent it.  */
  989.       dump_expr (TREE_OPERAND (t, 0), 0);
  990.       break;
  991.  
  992.     case TARGET_EXPR:
  993.       /* Note that this only works for G++ target exprs.  If somebody
  994.      builds a general TARGET_EXPR, there's no way to represent that
  995.      it initializes anything other that the parameter slot for the
  996.      default argument.  Note we may have cleared out the first
  997.      operand in expand_expr, so don't go killing ourselves.  */
  998.       if (TREE_OPERAND (t, 1))
  999.     dump_expr (TREE_OPERAND (t, 1), 0);
  1000.       break;
  1001.  
  1002.     case MODIFY_EXPR:
  1003.     case PLUS_EXPR:
  1004.     case MINUS_EXPR:
  1005.     case MULT_EXPR:
  1006.     case TRUNC_DIV_EXPR:
  1007.     case TRUNC_MOD_EXPR:
  1008.     case MIN_EXPR:
  1009.     case MAX_EXPR:
  1010.     case LSHIFT_EXPR:
  1011.     case RSHIFT_EXPR:
  1012.     case BIT_IOR_EXPR:
  1013.     case BIT_XOR_EXPR:
  1014.     case BIT_AND_EXPR:
  1015.     case BIT_ANDTC_EXPR:
  1016.     case TRUTH_ANDIF_EXPR:
  1017.     case TRUTH_ORIF_EXPR:
  1018.     case LT_EXPR:
  1019.     case LE_EXPR:
  1020.     case GT_EXPR:
  1021.     case GE_EXPR:
  1022.     case EQ_EXPR:
  1023.     case NE_EXPR:
  1024.       dump_binary_op (opname_tab[(int) TREE_CODE (t)], t);
  1025.       break;
  1026.  
  1027.     case CEIL_DIV_EXPR:
  1028.     case FLOOR_DIV_EXPR:
  1029.     case ROUND_DIV_EXPR:
  1030.       dump_binary_op ("/", t);
  1031.       break;
  1032.  
  1033.     case CEIL_MOD_EXPR:
  1034.     case FLOOR_MOD_EXPR:
  1035.     case ROUND_MOD_EXPR:
  1036.       dump_binary_op ("%", t);
  1037.       break;
  1038.  
  1039.     case COMPONENT_REF:
  1040.       dump_binary_op (".", t);
  1041.       break;
  1042.  
  1043.     case CONVERT_EXPR:
  1044.       dump_unary_op ("+", t, nop);
  1045.       break;
  1046.  
  1047.     case ADDR_EXPR:
  1048.       if (TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
  1049.       || TREE_CODE (TREE_OPERAND (t, 0)) == STRING_CST)
  1050.     dump_expr (TREE_OPERAND (t, 0), 0);
  1051.       else
  1052.     dump_unary_op ("&", t, nop);
  1053.       break;
  1054.  
  1055.     case INDIRECT_REF:
  1056.       if (TREE_HAS_CONSTRUCTOR (t))
  1057.     {
  1058.       t = TREE_OPERAND (t, 0);
  1059.       my_friendly_assert (TREE_CODE (t) == CALL_EXPR, 237);
  1060.       dump_expr (TREE_OPERAND (t, 0), 0);
  1061.       OB_PUTC ('(');
  1062.       dump_expr_list (TREE_CHAIN (TREE_OPERAND (t, 1)));
  1063.       OB_PUTC (')');
  1064.     }
  1065.       else
  1066.     dump_unary_op ("*", t, nop);
  1067.       break;
  1068.  
  1069.     case NEGATE_EXPR:
  1070.     case BIT_NOT_EXPR:
  1071.     case TRUTH_NOT_EXPR:
  1072.     case PREDECREMENT_EXPR:
  1073.     case PREINCREMENT_EXPR:
  1074.       dump_unary_op (opname_tab [(int)TREE_CODE (t)], t, nop);
  1075.       break;
  1076.  
  1077.     case POSTDECREMENT_EXPR:
  1078.     case POSTINCREMENT_EXPR:
  1079.       OB_PUTC ('(');
  1080.       dump_expr (TREE_OPERAND (t, 0), 0);
  1081.       OB_PUTCP (opname_tab[(int)TREE_CODE (t)]);
  1082.       OB_PUTC (')');
  1083.       break;
  1084.  
  1085.     case NON_LVALUE_EXPR:
  1086.       /* FIXME: This is a KLUDGE workaround for a parsing problem.  There
  1087.      should be another level of INDIRECT_REF so that I don't have to do
  1088.      this.  */
  1089.       if (NEXT_CODE (t) == POINTER_TYPE)
  1090.     {
  1091.       tree next = TREE_TYPE (TREE_TYPE (t));
  1092.  
  1093.       while (TREE_CODE (next) == POINTER_TYPE)
  1094.         next = TREE_TYPE (next);
  1095.       
  1096.       if (TREE_CODE (next) == FUNCTION_TYPE)
  1097.         {
  1098.           if (!nop) OB_PUTC ('(');
  1099.           OB_PUTC ('*');
  1100.           dump_expr (TREE_OPERAND (t, 0), 1);
  1101.           if (!nop) OB_PUTC (')');
  1102.           break;
  1103.         }
  1104.       /* else FALLTHRU */
  1105.     }
  1106.       dump_expr (TREE_OPERAND (t, 0), 0);
  1107.       break;
  1108.  
  1109.     case NOP_EXPR:
  1110.       dump_expr (TREE_OPERAND (t, 0), nop);
  1111.       break;
  1112.  
  1113.     case CONSTRUCTOR:
  1114.       OB_PUTC ('{');
  1115.       dump_expr_list (CONSTRUCTOR_ELTS (t), 0);
  1116.       OB_PUTC ('}');
  1117.       break;
  1118.  
  1119.       /*  This list is incomplete, but should suffice for now.
  1120.       It is very important that `sorry' does not call
  1121.       `report_error_function'.  That could cause an infinite loop.  */
  1122.     default:
  1123.       sorry ("`%s' not supported by dump_expr",
  1124.          tree_code_name[(int) TREE_CODE (t)]);
  1125.  
  1126.       /* fall through to ERROR_MARK...  */
  1127.     case ERROR_MARK:
  1128.     error:
  1129.       OB_PUTCP ("/* error */");
  1130.       break;
  1131.     }
  1132. }
  1133.  
  1134. static void
  1135. dump_binary_op (opstring, t)
  1136.      char *opstring;
  1137.      tree t;
  1138. {
  1139.   OB_PUTC ('(');
  1140.   dump_expr (TREE_OPERAND (t, 0), 1);
  1141.   OB_PUTC (' ');
  1142.   OB_PUTCP (opstring);
  1143.   OB_PUTC (' ');
  1144.   dump_expr (TREE_OPERAND (t, 1), 1);
  1145.   OB_PUTC (')');
  1146. }
  1147.  
  1148. static void
  1149. dump_unary_op (opstring, t, nop)
  1150.      char *opstring;
  1151.      tree t;
  1152.      int nop;
  1153. {
  1154.   if (!nop) OB_PUTC ('(');
  1155.   OB_PUTCP (opstring);
  1156.   dump_expr (TREE_OPERAND (t, 0), 1);
  1157.   if (!nop) OB_PUTC (')');
  1158. }
  1159.  
  1160. char *
  1161. fndecl_as_string (cname, fndecl, print_ret_type_p)
  1162.      tree cname, fndecl;
  1163.      int print_ret_type_p;
  1164. {
  1165.   return decl_as_string (fndecl, print_ret_type_p);
  1166. }
  1167.  
  1168. /* Same, but handtype a _TYPE.
  1169.    Called from convert_to_reference, mangle_class_name_for_template,
  1170.    build_unary_op, and GNU_xref_decl.  */
  1171. char *
  1172. type_as_string (typ, v)
  1173.      tree typ;
  1174.      int v;
  1175. {
  1176.   OB_INIT ();
  1177.  
  1178.   dump_type (typ, v);
  1179.  
  1180.   OB_FINISH ();
  1181.  
  1182.   return (char *)obstack_base (&scratch_obstack);
  1183. }
  1184.  
  1185. char *
  1186. expr_as_string (decl, v)
  1187.      tree decl;
  1188.      int v;
  1189. {
  1190.   OB_INIT ();
  1191.  
  1192.   dump_expr (decl, 1);
  1193.  
  1194.   OB_FINISH ();
  1195.  
  1196.   return (char *)obstack_base (&scratch_obstack);
  1197. }
  1198.  
  1199. /* A cross between type_as_string and fndecl_as_string.
  1200.    Only called from substitute_nice_name.  */
  1201. char *
  1202. decl_as_string (decl, v)
  1203.      tree decl;
  1204.      int v;
  1205. {
  1206.   OB_INIT ();
  1207.  
  1208.   dump_decl (decl, v);
  1209.  
  1210.   OB_FINISH ();
  1211.  
  1212.   return (char *)obstack_base (&scratch_obstack);
  1213. }
  1214.  
  1215. char *
  1216. cp_file_of (t)
  1217.      tree t;
  1218. {
  1219.   if (TREE_CODE (t) == PARM_DECL)
  1220.     return DECL_SOURCE_FILE (DECL_CONTEXT (t));
  1221.   else
  1222.     return DECL_SOURCE_FILE (t);
  1223. }
  1224.  
  1225. int
  1226. cp_line_of (t)
  1227.      tree t;
  1228. {
  1229.   if (TREE_CODE (t) == PARM_DECL)
  1230.     return DECL_SOURCE_LINE (DECL_CONTEXT (t));
  1231.   else
  1232.     return DECL_SOURCE_LINE (t);
  1233. }
  1234.  
  1235. char *
  1236. code_as_string (c, v)
  1237.      enum tree_code c;
  1238.      int v;
  1239. {
  1240.   return tree_code_name [c];
  1241. }
  1242.  
  1243. char *
  1244. language_as_string (c, v)
  1245.      enum languages c;
  1246.      int v;
  1247. {
  1248.   switch (c)
  1249.     {
  1250.     case lang_c:
  1251.       return "C";
  1252.  
  1253.     case lang_cplusplus:
  1254.       return "C++";
  1255.  
  1256.     default:
  1257.       my_friendly_abort (355);
  1258.     }
  1259. }
  1260.